package com.atwisdom.star.core.dao.pojo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.common.typeHandler.MyFastjsonTypeHandler;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 
 * @TableName report
 */
@TableName(value ="report",autoResultMap = true)
@Data
public class Report  extends BaseEntity  {
    /**
     * 报表标识
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 报表名称
     */
    @TableField(value = "name")
    private String name;

    /**
     * 报表类型
     */
    @TableField(value = "type")
    private String type;

    /**
     * 报表业务分类
     */
    @TableField(value = "category")
    private String category;

    /**
     * 所属组件标识
     */
    @TableField(value = "componentId")
    private String componentId;

    /**
     * 页面视图配置内容
     */
    @TableField(value = "value",typeHandler = MyFastjsonTypeHandler.class)
    private JSONArray value;

    /**
     * 报表配置脚本
     */
    @TableField(value = "script")
    private String script;

    /**
     * 页面视图扩展字段
     */
    @TableField(value = "extendField",typeHandler = MyFastjsonTypeHandler.class)
    private JSONObject extendField;

    /**
     * 说明
     */
    @TableField(value = "comments")
    private String comments;

    /**
     * 逻辑删除
     */
    @TableField(value = "deleteFlag")
    private Boolean deleteFlag;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    public Report(){

    }

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public Report(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.name = jsonObject.getString("name");
        this.type = keys.contains("type") ? jsonObject.getString("type") : null;
        this.category = keys.contains("category") ? jsonObject.getString("category"):null;
        this.componentId = keys.contains("componentId") ? jsonObject.getString("componentId"):null;
        this.value = keys.contains("value") ? jsonObject.getJSONArray("value") : null;
        this.script = keys.contains("script") ? jsonObject.getString("script") : null;
        this.extendField = keys.contains("extendField") ? jsonObject.getJSONObject("extendField") : null;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
        this.deleteFlag = keys.contains("deleteFlag") ? jsonObject.getBoolean("deleteFlag") : null;
    }

    /**
     * 获取摘要数据列
     * @return 反回查询列
     */
    public static String[] fetchSimpleColumns() {
        List<String> columns = new ArrayList<>();
        columns.add("id");
        columns.add("name");
        columns.add("type");
        columns.add("category");
        columns.add("componentId");
        columns.add("createdBy");
        columns.add("createTime");
        columns.add("updatedBy");
        columns.add("updateTime");
        return columns.toArray(new String[0]);
    }

    /**
     * 将当前模型对象转成jsonObject
     *
     * @return 返回结果
     */
    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.id);
        JsonObject.put("name", this.name);
        JsonObject.put("type", this.type);
        JsonObject.put("category", this.category);
        JsonObject.put("componentId", this.componentId);
        JsonObject.put("value", this.value);
        JsonObject.put("script", this.script);
        JsonObject.put("extendField", this.extendField);
        JsonObject.put("comments", this.comments);
        JsonObject.put("createdBy", this.createdBy);
        JsonObject.put("createTime", this.createTime);
        JsonObject.put("updatedBy", this.updatedBy);
        JsonObject.put("updateTime", this.updateTime);
        return JsonObject;
    }
}